﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Net;
using System.Data.Services.Client;

namespace Data.AID
{
    public class Colaborador
    {
        public int Id { get; set; }
        public string Login { get; set; }
        public string Email { get; set; }
        public string Matricula { get; set; }
        public string Nome { get; set; }
        public string Funcao { get; set; }
        public string DataAdmissao { get; set; }
        public string GestorImediato { get; set; }
        public string Area { get; set; }
    }

    public class ColaboradorDAO
    {
        public List<Colaborador> GettAllEmployees()
        {
            /*Intranet Produção: http://srvext006:41237/ */
            List<Colaborador> colaboradores = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite mySite = new SPSite(Dados.UriSite))
                {
                    using (SPWeb myWeb = mySite.OpenWeb())
                    {
                        colaboradores = myWeb.Lists["Usuário"].Items.Cast<SPListItem>()
                            .Select(p => new Colaborador 
                            { 
                                Id = (int)p["ID"],
                                Login = p["Login"] == null ? "" : p["Login"].ToString(),
                                GestorImediato = p["GestorImediato"] == null ? "" : p["GestorImediato"].ToString(),
                                Email = p["Email"] == null ? "" : p["Email"].ToString(),
                                Matricula = p["Matricula"] == null ? "" : p["Matricula"].ToString(),
                                Area = p["Departamento"] == null ? "" : p["Departamento"].ToString(),
                                Funcao = p["Cargo"] == null ? "" : p["Cargo"].ToString(),
                                Nome = p["Nome"] == null ? "" : p["Nome"].ToString(),
                                DataAdmissao = p["DataAdmissao"] == null ? "" : p["DataAdmissao"].ToString()
                            })
                            .OrderBy(p => p.Id)
                            .ToList();                            
                    }
                }
            });

            return colaboradores;
            
        }
        public Colaborador GetEmployeeByLogin(string loginName)
        {
            return GettAllEmployees().Where(p => p.Login.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)[1] == loginName.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries)[1]).SingleOrDefault();
        }
        public Colaborador GetEmployeeByRegistration(string registration)
        {
            return GettAllEmployees().Where(p => p.Matricula == registration).SingleOrDefault();
        }
        public Colaborador GetEmployeeById(int Id)
        {
            return GettAllEmployees().Where(p => p.Id == Id).SingleOrDefault();
        }
        public Colaborador GetEmployeeByName(string name)
        {
            return GettAllEmployees().Where(p => p.Nome.ToLower() == name.ToLower()).SingleOrDefault();
        }
    }
}
